package com.specter.mvc.model.batis;

import com.specter.utils.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * Note:数据库方言工厂类
 * 
 * @author Liang.Wang
 * @version Mar 16, 2017
 */
@Slf4j
public class DialectFactory {

	private DialectFactory() {
	}

	public static Dialect getDialect(String databaseId) {
		Dialect dialect = null;
		if (StringUtils.isNotEmpty(databaseId)) {
			String dialectClazz = DialectFactory.class.getPackageName() + ".Dialect4" + databaseId;
			try {
				Class<?> clazz = Class.forName(dialectClazz);
				if (Dialect.class.isAssignableFrom(clazz)) {
					dialect = (Dialect) clazz.getDeclaredConstructor().newInstance();
				}
			} catch (Exception e) {
				log.error("Class : {} is not found", dialectClazz);
			}
		}

		/* 未配置方言则抛出异常 */
		if (dialect == null) {
			log.error("The value of the dialect property in mybatis configuration.xml is not defined.");
		}
		return dialect;
	}
}
